package nju.bishe.weibo;

import java.util.List;

import nju.bishe.weibo.data.UserToken;
import nju.bishe.weibo.data.db.UserTokenTable;
import nju.bishe.weibo.task.GenericTask;
import nju.bishe.weibo.task.TaskAdapter;
import nju.bishe.weibo.task.TaskListener;
import nju.bishe.weibo.task.TaskParams;
import nju.bishe.weibo.task.TaskResult;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;

/**
 * 
 * @author longwei.wu modified by gerald.xv at gmail.com
 * 加载界面
 * 修改：将内部handler实现改为task实现
 */
public class LaunchActivity extends Activity {
	Intent intent;
	GenericTask mLaunchTask;
	private TaskListener mLaunchListener = new TaskAdapter() {
		@Override
		public void onPreExecute(GenericTask task) {
			setContentView(R.layout.launch);
		}

		@Override
		public void onPostExecute(GenericTask task, TaskResult result) {
			startActivity(intent);
		}

		@Override
		public String getName() {
			return "LaunchTask";
		}
	};

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);

		if (mLaunchTask != null
				&& mLaunchTask.getStatus() == GenericTask.Status.RUNNING) {
			return;
		} else {
			mLaunchTask = new SplashTask();
			mLaunchTask.setListener(mLaunchListener);
			mLaunchTask.execute();
		}
		
	}

	private class SplashTask extends GenericTask {

		@Override
		protected TaskResult _doInBackground(TaskParams... params) {

			UserTokenTable userToken = new UserTokenTable(LaunchActivity.this);
			List<UserToken> users = userToken.getAllUsersRemember();

			if (users.size() == 0) {
				intent = new Intent(LaunchActivity.this,
						FirstLoginActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

			} else {
				intent = new Intent(LaunchActivity.this, LoginActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

			}
			return TaskResult.OK;
		}
	}

}
